dojo.provide("dojot.album.AlbumDialog");
dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");
dojo.require("dojo.i18n");
dojo.requireLocalization("dojot", "album");
dojo.requireLocalization("dojot", "common");


dojo.declare("dojot.album.util",null,{
	constructor:function(obj){
		this.message = dojo.i18n.getLocalization("dojot", "album",dojo.locale)
		dojo.mixin(this.message,dojo.i18n.getLocalization("dojot", "common",dojo.locale));
		this.fadeOut = dojo.fadeOut({node: "errors",duration: 1000});
		this.fadeIn = dojo.fadeIn({node: "errors",duration: 1000});
		this.clueInfo = function(clueId,clueInfo){
			dojo.byId(clueId).innerHTML = clueInfo;
			dojo.style(clueId,'display','block');
			currentAnimation = dojo.fx.chain([this.fadeIn,this.fadeOut]);
			currentAnimation.play();
		};
		this.getIds = function(){		
			var temp = [];
			dojo.query("input:checked").forEach(function(data){
			    temp.push(data.value);
			});
			if(temp == ""){
		 		this.clueInfo('errors',this.message.album2AlbumDialogjsSelectedOnePic);
				return "";
		 	}else return temp;
		};
		this.postService = function(url,loadSuccessFn){	
			dojo.xhrPost({
			url:url, 
			load:function(responseDate){
				if(responseDate.success){
				//	deleteNode(picIds);	
					loadSuccessFn();			
				}else{
					this.clueInfo('errors',this.message.errorInfo);
				}
			},
			handleAs: "json"
			})
		}
	}
});
dojo.declare("dojot.album.albumTransferDialog",dojot.album.util,{
	albumId:"",
	getListUrl:"",
	transferUrl:"",
	galleryParams:"",
	constructor:function(obj){
		this.albumId = obj.albumId;
		this.getListUrl = obj.getListUrl;
	  	this.transferUrl = obj.transferUrl;			
		dojo.query("#"+obj.transferId).onclick(dojo.hitch(this, "transferPic"));
		dojo.connect(dijit.byId("chooseAlbum"),"execute",dojo.hitch(this,"postChooseAlbum"));
		var that = this;		
		dojo.subscribe("albumTransfer",function(myGallery){	
		console.log(myGallery.picId);		
			that.galleryParams = {
				callback: function(){
					myGallery.gallery.deleteCurrentImage();
				},
				picId: myGallery.picId
			};
			console.log("----"+that.galleryParams.picId);	
			that.getAlbumJsonList();	
		});
	},
	postChooseAlbum:function(){
		var that = this;
		var albumIdB = dojo.byId('combo').value;
		var picId = this.galleryParams.picId || that.getIds();
		var urladdr = this.transferUrl+"&ids="+picId+"&albumIdB="+albumIdB;
		var callback = this.galleryParams.callback;
		var loadSuccessFn = function(){
			if (callback) {
				callback();
			}else{
				dojo.query("input:checked").forEach(function(data){
					data.checked = false;
				});
				window.location.reload();
			}				
		}	
		that.postService(urladdr,loadSuccessFn);		
	},
	transferPic:function(){
		var picIds = this.getIds();
		if(picIds != ""){
			this.getAlbumJsonList();				
		}
	},
	getAlbumJsonList:function(callback){			
		 dojo.xhrGet({
			 url:this.getListUrl, 
			 load:dojo.hitch(this,"insertOption"),
			 handleAs: "json"
		});
		dojo.query('#chooseAlbum').style("display:block");
		dijit.byId('chooseAlbum').show();		
	},
	insertOption:function(arguments){ // 锟斤拷始			
		var items=arguments;
		var combo = dojo.byId("combo");
		var index = 0;
		for(var i=0; i<items.length; i++){
			if(this.albumId!=items[i].id){					
				var oOption=new Option(items[i].name,items[i].id);
		  		combo.options[index]=oOption;
		  		index++;
			}
		 
		}
	}	
});
		
dojo.declare("dojot.album.albumPicDelete",dojot.album.util,{
//	picIds:"",
	albumId:"",
	deletePicURL:"",
	constructor:function(obj){
	//	this.picIds = dojo.isFunction(obj.picIds)? obj.picIds():obj.PicIds;
	//	this.picIds = getIds();
		this.albumId = obj.albumId;
		this.deletePicURL = obj.deletePicURL;
		dojo.query("#deletePic").onclick(dojo.hitch(this, "confirmDelete"))
	//	dojo.query("#deletePic").onclick(dojo.hitch(this, "deletePic"));
		var that = this;
		dojo.subscribe("albumPicDelete",function(myGallery){	
			that.deletePic(myGallery.picId,function(){
				myGallery.gallery.deleteCurrentImage();
			});
		})
	},
	confirmDelete:function(){
		var picIds = this.getIds();
		if(picIds!=""){	
			if(confirm(this.message.confirmDelete)){
			this.deletePic(picIds);
			}								
		}			
	},					
	deletePic:function(picIds,callback){		
		var url =this.deletePicURL+picIds;
		var loadSuccessFn = function(){
			if (callback) {
				callback();
			}else{
				dojo.query("input:checked").forEach(function(data){
					data.checked = false;
				});
				window.location.reload();
			}					
		}
		this.postService(url,loadSuccessFn);	
	}	
});

dojo.declare("dojot.album.albumDelete",dojot.album.util,{
	albumId:"",
	attr:"",
	//groupId:'',
	deleteUrl:'',
	returnUrl:'',
	constructor:function(obj){
		this.albumId = obj.albumId;
		this.attr = obj.attr;
		//this.groupId = obj.groupId;
		this.deleteUrl = obj.deleteUrl;
		this.returnUrl = obj.returnUrl;
		dojo.query(this.attr).onclick(dojo.hitch(this, 'deleteAlbum'));
	},						
	deleteAlbum:function(){
		var that = this;
		if(confirm(that.message.album2AlbumDelete)){
			var url = that.deleteUrl+that.albumId;
			var loadSuccessFn = function(){
				 window.location.href=that.returnUrl;	
			};
			that.postService(url,loadSuccessFn);			 		
	 	}
	}
})

